<template>
  <div class="activity-detail-container">
    <div v-if="activity" class="activity-detail">
      <div class="activity-header">
        <img :src="activity.images[0] || '/default-activity.png'" alt="活动图片">
        <div class="activity-info">
          <h1>{{ activity.title }}</h1>
          <p class="activity-club">
            <span v-if="activity.club" @click="goToClubDetail(activity.club.id)">{{ activity.club.name }}</span>
            <span v-else>个人活动</span>
          </p>
          <p class="activity-time">{{ formatDate(activity.start_time) }} - {{ formatDate(activity.end_time) }}</p>
          <p class="activity-location">{{ activity.location }}</p>
          <p class="activity-participants">{{ activity.current_participants }} / {{ activity.max_participants }} 人已报名</p>
          <div class="activity-status" :class="[activity.status, (activity.current_participants >= activity.max_participants && activity.status === 'registration_open') ? 'full' : '']">
            {{ (activity.current_participants >= activity.max_participants && activity.status === 'registration_open') ? '已报满' : statusMap[activity.status] }}
          </div>
          <button class="sign-up-btn" v-if="canSignUp">立即报名</button>
          <button class="signed-up-btn" v-else-if="isSignedUp" disabled>已报名</button>
          <button class="closed-btn" v-else disabled>报名已截止</button>
        </div>
      </div>
      <div class="activity-description">
        <h2>活动介绍</h2>
        <div v-html="activity.description"></div>
      </div>
      <div class="activity-images" v-if="activity.images.length > 1">
        <h2>活动图片</h2>
        <div class="image-gallery">
          <img v-for="(image, index) in activity.images.slice(1)" :key="index" :src="image" alt="活动图片">
        </div>
      </div>
    </div>
    <div v-else class="loading">加载中...</div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { activityApi } from '@/api'
import { formatDate } from '@/utils/date'

const route = useRoute()
const router = useRouter()
const activityId = route.params.id
const activity = ref(null)
const isSignedUp = ref(false)

// 状态映射
const statusMap = {
  'not_started': '未开始报名',
  'registration_open': '报名中',
  'ongoing': '进行中',
  'completed': '已结束',
  'cancelled': '已取消'
}

// 是否可以报名
const canSignUp = computed(() => {
  if (!activity.value) return false
  return activity.value.status === 'registration_open' &&
         activity.value.current_participants < activity.value.max_participants &&
         !isSignedUp.value
})

// 获取活动详情
const fetchActivityDetail = async () => {
  try {
    const res = await activityApi.getActivityDetail(activityId)
    activity.value = res.data
    // 检查是否已报名
    isSignedUp.value = res.data.is_signed_up || false
  } catch (error) {
    console.error('获取活动详情失败:', error)
  }
}

// 报名活动
const signUpActivity = async () => {
  try {
    await activityApi.signUpActivity({ activity: activityId })
    isSignedUp.value = true
    activity.value.current_participants += 1
  } catch (error) {
    console.error('报名活动失败:', error)
  }
}

// 跳转到社团详情
const goToClubDetail = (clubId) => {
  router.push({ name: 'clubDetail', params: { id: clubId } })
}

// 页面加载时执行
onMounted(() => {
  fetchActivityDetail()
})
</script>

<style scoped>
.activity-detail-container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
}

.activity-header {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.activity-header img {
  width: 300px;
  height: 200px;
  object-fit: cover;
  border-radius: 8px;
}

.activity-info {
  flex: 1;
  min-width: 300px;
}

.activity-info h1 {
  font-size: 24px;
  margin-bottom: 10px;
}

.activity-club {
  margin-bottom: 8px;
}

.activity-club span {
  color: #42b983;
  cursor: pointer;
}

.activity-time,
.activity-location,
.activity-participants {
  color: #666;
  margin-bottom: 8px;
}

.activity-status {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  margin-bottom: 16px;
}

.activity-status.not_started {
  background-color: #f0f9eb;
  color: #67c23a;
}

.activity-status.registration_open {
  background-color: #e6f7ff;
  color: #1890ff;
}

.activity-status.full {
  background-color: #fef0f0;
  color: #e6a23c;
}

.activity-status.ongoing {
  background-color: #ecf5ff;
  color: #4096ff;
}

.activity-status.completed {
  background-color: #f5f5f5;
  color: #909399;
}

.activity-status.cancelled {
  background-color: #fef0f0;
  color: #f56c6c;
}

.sign-up-btn,
.signed-up-btn,
.closed-btn {
  padding: 10px 20px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.signed-up-btn,
.closed-btn {
  background-color: #e0e0e0;
  cursor: not-allowed;
}

.activity-description,
.activity-images {
  margin-bottom: 30px;
}

.activity-description h2,
.activity-images h2 {
  font-size: 20px;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid #eee;
}

.image-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px;
}

.image-gallery img {
  width: 100%;
  height: 150px;
  object-fit: cover;
  border-radius: 4px;
}

.loading {
  text-align: center;
  padding: 40px;
  color: #666;
}
</style>